/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.google.code.teamtools.client.view;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.code.teamtools.client.AppEvents;
import com.google.code.teamtools.client.RegistryKeys;
import com.google.code.teamtools.client.i18n.AppTextConstants;
import com.google.code.teamtools.client.widget.LoginDialog;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * 
 * @author David Kuhn <david.s.kuhn at gmail.com>
 */
public class AppView extends View
{
    public static final String WEST_PANEL = "west";
    public static final String VIEWPORT = "viewport";
    public static final String CENTER_PANEL = "center";

    private static final AppTextConstants APP_TEXT =
            (AppTextConstants) Registry.get(RegistryKeys.APP_TEXT);

    private Viewport viewport;
    private ContentPanel west;
    private LayoutContainer center;

    public AppView(Controller controller)
    {
        super(controller);
    }

    @Override
    protected void initialize()
    {
        LoginDialog dialog = new LoginDialog(APP_TEXT);
        dialog.setClosable(false);
        dialog.addListener(Events.Hide, new Listener<WindowEvent>()
        {
            public void handleEvent(WindowEvent be)
            {
                Dispatcher.forwardEvent(AppEvents.Init);
            }
        });
        dialog.show();
    }

    private void initUI()
    {
        viewport = new Viewport();
        viewport.setLayout(new BorderLayout());

        createNorth();
        createWest();
        createCenter();

        // registry serves as a global context
        Registry.register(VIEWPORT, viewport);
        Registry.register(WEST_PANEL, west);
        Registry.register(CENTER_PANEL, center);

        RootPanel.get().add(viewport);
    }

    private void createNorth()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("<div id='app-header' class='x-small-editor'>");
        sb.append("<div id=app-title>");
        sb.append(APP_TEXT.appTitle());
        sb.append("</div></div>");

        HtmlContainer northPanel = new HtmlContainer(sb.toString());
        northPanel.setStateful(false);

        BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
        data.setMargins(new Margins());
        viewport.add(northPanel, data);
    }

    private void createWest()
    {
        BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 200, 150, 350);
        data.setSplit(true);  
        data.setCollapsible(true); 
        data.setMargins(new Margins(5, 0, 5, 5));

        west = new ContentPanel();
        west.setBodyBorder(false);
        west.setLayout(new AccordionLayout());
        west.setLayoutOnChange(true);
        west.setHeading(APP_TEXT.appWestPanelTitle());

        viewport.add(west, data);
    }

    private void createCenter()
    {
        center = new LayoutContainer();
        center.setLayout(new FitLayout());

        BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
        data.setMargins(new Margins(5, 5, 5, 5));

        viewport.add(center, data);
    }

    protected void handleEvent(AppEvent event)
    {
        if (event.getType() == AppEvents.Init) {
            initUI();
        }
    }
}
